package com.js.springbootredissignin.controller;


import com.js.springbootredissignin.entity.Student;
import com.js.springbootredissignin.entity.StudentPojo;
import com.js.springbootredissignin.service.StudentService;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
public class StudentController {

    @Autowired
    private StudentService studentService;


    @GetMapping("/selectAllIT")
    @ResponseBody
    public Set<Student> selectAllIT(Model model){
        Set<Student> list = studentService.selectAllIT();
        return list;
    }
    @ResponseBody
    @GetMapping("/selectSignInCount")
    public Map selectSignInCount(){
        Map<String,Object> map = new HashMap<>();
        int sum = studentService.sum();
        int count = studentService.selectCountIT();
        map.put("sum",sum);
        map.put("count",count);
        return map;
    }

    @PostMapping("/add")
    @ResponseBody
    public int add(String name){
        System.out.println("值："+name);
        int result = studentService.SignIn(name);
        return result;
    }


    //处理文件上传
    @ResponseBody//返回json数据
    @RequestMapping(value = "/excelImport", method = RequestMethod.POST)
    public String uploadImg(@RequestParam("file") MultipartFile file,HttpServletRequest request) {
        String contentType = file.getContentType();
        String fileName = file.getOriginalFilename();
        if (file.isEmpty()) {
            return "文件为空！";
        }
        try {
            //根据路径获取这个操作excel的实例
            HSSFWorkbook wb = new HSSFWorkbook(file.getInputStream());
            //根据页面index 获取sheet页
            HSSFSheet sheet = wb.getSheetAt(0);
            //实体类集合
            List<Student> importDatas = new ArrayList<>();
            HSSFRow row = null;
            //循环sesheet页中数据从第二行开始，第一行是标题
            for (int i = 1; i < sheet.getPhysicalNumberOfRows(); i++) {
            //获取每一行数据
                row = sheet.getRow(i);
                Student student = new Student();
                //Excel表格数据的获取
                student.setId(Integer.valueOf((int) row.getCell(0).getNumericCellValue()));
                student.setName(row.getCell(1).getStringCellValue());
                student.setStatus(Integer.valueOf((int) row.getCell(2).getNumericCellValue()));
                importDatas.add(student);
            }
            //循环展示导入的数据，实际应用中应该校验并存入数据库
            for (Student student : importDatas) {
                System.out.println("你好世界");
                System.out.println("ID:"+student.getId()+" name:"+student.getName()+" status:"+student.getStatus());
                int result = studentService.add(student);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "导入成功!";
    }

    @PutMapping("/updateSignIn")
    @ResponseBody
    public int updateSignIn(){
        int result = studentService.updateSignIn();
        return result;
    }

    @GetMapping("/noSignIn")
    @ResponseBody
    public Map noSignIn(){
        Map<String,Object> map = new HashMap<>();
        StudentPojo student = studentService.selectNoCount();
        List<Student> list = studentService.noSignIn();
        int result = studentService.sum();
        int noCount=student.getNoCount();
        map.put("result",result);
        map.put("list",list);
        map.put("noCount",noCount);
        return map;
    }
}
